package com.canbot.u05.activity.guide;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.guide.MapPointInfo;
import com.canbot.u05.bean.guide.MapReachVoice;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.helper.DialogBuildHelper;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.socket.StringMsgBean;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * @date 2018/5/22
 * 导览任务点设置
 */

public class GuideSetPointerActivity extends BaseActivityNoTitle {

        @BindView(R.id.main_iv_back)
        Button mainIvBack;

        @BindView(R.id.pointer_name_tv)
        TextView pointerNameTv;

        @BindView(R.id.pointer_name_iv)
        Button pointerNameIv;

        @BindView(R.id.pointer_save_bt)
        Button pointerSaveBt;

        @BindView(R.id.pointer_rcv)
        RecyclerView pointerRcv;

        @BindView(R.id.pointer_play_iv)
        ImageView pointerPlayIv;

        @BindView(R.id.pointer_delete_iv)
        ImageView pointerDeleteIv;

        @BindView(R.id.run_click_view)
        View runClickView;

        private RecyclerAdapter recyclerAdapter;

        private ItemTouchHelper itemTouchHelper;

        private List<MapReachVoice> mapReachVoices;

        private MapPointInfo mapPointInfo;

        private DialogBuildHelper.SetNameTipBuilder renameDialog;

        private ToastTools toastTools;

        private int currentPosition;

        private int runPosition = -1;

        public static final int REQ_CODE_OVERLAY = 250;

        public static final int REQ_REACH_CODE = 251;

        public static final int REQ_ADD_VOICE = 252;

        public static final int REQ_ADD_ACTION = 253;

        public static final int REQ_ADD_PIC = 254;

        private DialogBuildHelper.TipBuilder hintDialog;

        private MapReachVoice mapReachVoice;

        private boolean isFirstSetAction;

        private boolean isFirstLanguageVoice;

        private boolean isFirstSave;

        private boolean isFirstSetPic;
        private DialogBuildHelper.TipBuilder setPicDialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.guide_point_set_activity);
                EventBus.getDefault().register(this);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                //检查权限WindowManager 6.0权限检测
                if (!Settings.canDrawOverlays(this)) {
                        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                        startActivityForResult(intent, REQ_CODE_OVERLAY);
                        return;
                }
                if (GuideMapAddTaskActivity.isFirstAdd) {
                        isFirstSetAction = true;
                        isFirstLanguageVoice = true;
                        isFirstSetPic = true;
                        isFirstSave = true;
                } else {
                        isFirstSetAction = false;
                        isFirstLanguageVoice = false;
                        isFirstSetPic = false;
                        isFirstSave = false;
                }
                initView();
        }

        /**
         * 消息发布者位置
         * ReceiveData
         * mHandler
         * case MsgType.MOBILE_PAD_MAP_TASK:
         *
         * @param bean StringMsgBean
         */
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMapEvent(StringMsgBean bean) {
                switch (bean.getMsgType()) {
                        case MsgType.COMPERE_RAN_TASK_END:
                                toastTools.showText(getString(R.string.end_task_preview));
                                runPosition = -1;
                                pointerPlayIv.setSelected(false);
                                runClickView.setVisibility(View.GONE);
                                recyclerAdapter.notifyDataSetChanged();
                                break;
                        case MsgType.COMPERE_RAN_TASK_STATE:
                                String msgData = bean.getMsgData();
                                toastTools.showText("msgData:" + msgData);
                                break;
                        case MsgType.COMPERE_RAN_TASK_POSITION:
                                msgData = bean.getMsgData();
                                toastTools.showText(getString(R.string.executor_node) + msgData);
                                runPosition = Integer.valueOf(msgData);
                                recyclerAdapter.notifyDataSetChanged();
                                break;
                        default:
                                break;

                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == REQ_CODE_OVERLAY) {
                        if (Settings.canDrawOverlays(this)) {
                                Log.e("onActivityResult", "resultCode:" + true);
                                initView();
                        } else {
                                toastTools.showText(getString(R.string.permission_denied));
                                finish();
                        }
                        return;
                }
                if (data == null) {
                        return;
                }
                if (requestCode == REQ_REACH_CODE && resultCode == RESULT_OK) {
                        String reachInfo = data.getStringExtra("reachInfo");
                        MapReachVoice mapReachVoice = JSON.parseObject(reachInfo, MapReachVoice.class);
                        if (currentPosition == -1) {
                                mapReachVoices.add(mapReachVoice);
                        } else {
                                mapReachVoices.remove(currentPosition);
                                mapReachVoices.add(currentPosition, mapReachVoice);
                        }
                        recyclerAdapter.notifyDataSetChanged();
                }

                if (requestCode == REQ_ADD_VOICE && resultCode == RESULT_OK) {
                        int type = data.getIntExtra("type", -1);
                        String audioPath = data.getStringExtra("path");
                        String content = data.getStringExtra("content");
                        mapReachVoice = new MapReachVoice();
                        mapReachVoice.setReachVoiceType(type);
                        mapReachVoice.setReachVoiceContent(audioPath);
                        int size = mapReachVoices.size() + 1;
                        mapReachVoice.setReachName(size > 9 ? size + "" : "0" + size);
                        mapReachVoices.add(mapReachVoice);
                        recyclerAdapter.notifyDataSetChanged();
                        if (isFirstSetAction) {
                                isFirstSetAction = false;
                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(29));
                        }
                        startForResultActivity(GuideActionSetActivity.class, REQ_ADD_ACTION);
                        return;
                }

                if (requestCode == REQ_ADD_ACTION && resultCode == RESULT_OK) {
                        String actionID = data.getStringExtra("actionID");
                        String actionName = data.getStringExtra("actionName");
                        mapReachVoice.setReachActionId(actionID);
                        mapReachVoice.setReachActionName(actionName);
                        recyclerAdapter.notifyDataSetChanged();
                        if (isFirstSetPic) {
                                isFirstSetPic = false;
                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(30));
                        }
                        showSetPicDialog();
                        return;
                }
                if (requestCode == REQ_ADD_PIC && resultCode == RESULT_OK) {
                        String picPath = data.getStringExtra("picPath");
                        mapReachVoice.setImagePath(picPath);
                        recyclerAdapter.notifyDataSetChanged();
                        if (isFirstSave) {
                                isFirstSave = false;
                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(28));
                        }
                }
        }

        private void showSetPicDialog() {
                if (setPicDialog == null) {
                        setPicDialog = DialogBuildHelper.createDialogTip(this)
                                .setTitle(getString(R.string.image_settings))
                                .setLeftButtonText("不设置")
                                .setRightButtonText("去设置")
                                .setClickListener(new DialogBuildHelper.ITipClickListener() {
                                        @Override
                                        protected void sure() {
                                                Intent picIntent = new Intent(GuideSetPointerActivity.this, GuideChoosePicActivity.class);
                                                picIntent.putExtra("picPath", mapReachVoice.getImagePath());
                                                startForResultActivity(picIntent, REQ_ADD_PIC);
                                        }

                                        @Override
                                        protected void cancel() {
                                                mapReachVoice.setImagePath(null);
                                                recyclerAdapter.notifyDataSetChanged();
                                                if (isFirstSave) {
                                                        isFirstSave = false;
                                                        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(28));
                                                }
                                        }
                                }, true).setContent("是否需要设置讲解是的图片?", Gravity.CENTER);
                        ;
                }
                setPicDialog.show();
        }

        private void initView() {
                runClickView.setVisibility(View.GONE);
                String pointInfo = getIntent().getStringExtra("pointInfo");
                mapPointInfo = JSONObject.parseObject(pointInfo, MapPointInfo.class);
                Collections.sort(mapPointInfo.getReachVoices(), (lhs, rhs) -> lhs.getIndex() - rhs.getIndex());
                windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
                pointerRcv.setLayoutManager(new GridLayoutManager(getApplicationContext(), 5, GridLayoutManager.VERTICAL, false));
                mapReachVoices = this.mapPointInfo.getReachVoices();
                recyclerAdapter = new RecyclerAdapter(this);
                itemTouchHelper = new ItemTouchHelper(new MyCallBack(recyclerAdapter));
                itemTouchHelper.attachToRecyclerView(pointerRcv);
                pointerRcv.setAdapter(recyclerAdapter);
                pointerNameTv.setText(this.mapPointInfo.getOtherName());
        }

        private Dialog dialogLoading;

        @OnClick({R.id.main_iv_back, R.id.pointer_name_iv, R.id.pointer_save_bt, R.id.pointer_play_iv, R.id.pointer_delete_iv, R.id.run_click_view})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.main_iv_back:
                                if (mapReachVoices == null || mapReachVoices.size() == 0) {
                                        finish();
                                } else {
                                        showHintDialog();
                                }
                                break;
                        case R.id.pointer_name_iv:
                                showRenameDialog();
                                break;
                        case R.id.run_click_view:
                                toastTools.showText(getString(R.string.please_stop_preview));
                                break;
                        case R.id.pointer_save_bt:
                                if (mapReachVoices == null || mapReachVoices.size() == 0) {
                                        toastTools.showText(getString(R.string.add_content));
                                        return;
                                }
                                Intent result = new Intent();
                                for (int i = 0; i < mapReachVoices.size(); i++) {
                                        mapReachVoices.get(i).setIndex(i);
                                }
                                result.putExtra("pointInfo", JSON.toJSONString(mapPointInfo));
                                setResult(RESULT_OK, result);
                                finish();
                                break;
                        case R.id.pointer_play_iv:
                                if (mapReachVoices == null || mapReachVoices.size() == 0) {
                                        toastTools.showText(getString(R.string.add_content));
                                        return;
                                }
                                String data;
                                int type;
                                if (pointerPlayIv.isSelected()) {
                                        data = "stop";
                                        type = MAP_TYPE_REACH_PREVIEW_STOP;
                                } else {
                                        data = JSON.toJSONString(mapReachVoices);
                                        type = MAP_TYPE_REACH_PREVIEW_TYPE;
                                }
                                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                                sendMsg2Chest(type, data);
                                break;
                        case R.id.pointer_delete_iv:
                                toastTools.showText(getString(R.string.drag_the_content));
                                break;
                        default:
                                break;
                }
        }

        public static final int MAP_TYPE_REACH_PREVIEW_TYPE = 19;

        public static final int MAP_TYPE_REACH_PREVIEW_STOP = 20;

        private void sendMsg2Chest(int type, String data) {
                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", type + "");
                hashMap.put("data", data);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                pointerPlayIv.setSelected(!pointerPlayIv.isSelected());
                                                runClickView.setVisibility(pointerPlayIv.isSelected() ? View.VISIBLE : View.GONE);
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText(getString(R.string.network_error));
                                LoadingDialogUtils.closeDialog(dialogLoading);
                        }
                });
        }


        /**
         * 提示Dialog
         */
        private void showRenameDialog() {
                if (renameDialog == null) {
                        renameDialog = DialogBuildHelper.createDialogSetName(this)
                                .setTitle(getString(R.string.task_point_set_name))
                                .setHintTextTv("", false)
                                .setClickListener(new DialogBuildHelper.ISetNameClickListener() {
                                        @Override
                                        protected void sure(String trim, TextView tipTextView) {
                                                if (TextUtils.isEmpty(trim)) {
                                                        toastTools.showText(getString(R.string.please_enter_content));
                                                        tipTextView.setText(getString(R.string.please_enter_content));
                                                        return;
                                                }
                                                mapPointInfo.setOtherName(trim);
                                                pointerNameTv.setText(trim);
                                                renameDialog.dismiss();
                                        }
                                }, false);
                }
                renameDialog.setContent(mapPointInfo.getOtherName());
                renameDialog.show();
        }

        /**
         * 提示Dialog
         */
        private void showHintDialog() {
                if (hintDialog == null) {
                        hintDialog = DialogBuildHelper.createDialogTip(this)
                                .setContent(getString(R.string.task_exit_hint), Gravity.CENTER)
                                .setClickListener(new DialogBuildHelper.ITipClickListener() {
                                        @Override
                                        protected void sure() {
                                                finish();
                                        }
                                }, true);
                }
                hintDialog.show();
        }

        class RecyclerAdapter extends RecyclerView.Adapter<MyHolder> {

                private final LayoutInflater layoutInflater;

                RecyclerAdapter(Context context) {
                        this.layoutInflater = LayoutInflater.from(context);
                }

                @NonNull
                @Override
                public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        View inflate = null;
                        switch (viewType) {
                                case 0:
                                        inflate = layoutInflater.inflate(R.layout.guide_reach_item, parent, false);
                                        break;
                                case 1:
                                        inflate = layoutInflater.inflate(R.layout.guide_reach_item2, parent, false);
                                        break;
                                default:
                        }

                        return new MyHolder(inflate, viewType);
                }

                @Override
                public void onBindViewHolder(@NonNull MyHolder holder, int position) {
                        if (getItemViewType(position) == 1) {
                                return;
                        }
                        MapReachVoice mapReachVoice = mapReachVoices.get(position);
                        holder.updateView(mapReachVoice, getItemViewType(position), position);
                }

                @Override
                public int getItemCount() {
                        return mapReachVoices.size() + 1;
                }

                @Override
                public int getItemViewType(int position) {
                        return position == mapReachVoices.size() ? 1 : 0;
                }
        }

        class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

                private TextView nameTv;

                private TextView indexTv;

                private int position;

                private ImageView roundedImageView;

                public MyHolder(View itemView, int viewType) {
                        super(itemView);
                        switch (viewType) {
                                case 0:
                                        indexTv = (TextView) itemView.findViewById(R.id.point_item_index_tv);
                                        nameTv = (TextView) itemView.findViewById(R.id.point_item_name_tv);
                                        roundedImageView = itemView.findViewById(R.id.point_item_riv);
                                        break;
                                case 1:
                                        break;
                                default:
                        }

                        itemView.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                        if (getItemViewType() == 1) {
                                                return false;
                                        }
                                        itemTouchHelper.startDrag(MyHolder.this);
                                        showWindowView(MyHolder.this.itemView);
                                        return false;
                                }
                        });
                        itemView.setOnClickListener(this);
                }

                @Override
                public void onClick(View v) {
                        if (getItemViewType() == 1) {
                                currentPosition = -1;
                                Intent intent = new Intent(GuideSetPointerActivity.this, GuideVoiceSetActivity.class);
                                if (isFirstLanguageVoice) {
                                        isFirstLanguageVoice = false;
                                        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(27));
                                }
                                startForResultActivity(intent, REQ_ADD_VOICE);
                        } else {
                                currentPosition = position;
                                Intent intent = new Intent(GuideSetPointerActivity.this, GuideReachSetActivity.class);
                                intent.putExtra("reachInfo", JSON.toJSONString(mapReachVoices.get(position)));
                                startForResultActivity(intent, REQ_REACH_CODE);
                        }
                }

                public void updateView(MapReachVoice mapReachVoice, int itemViewType, int position) {
                        this.position = position;
                        switch (itemViewType) {
                                case 0:
                                        int i = position + 1;
                                        indexTv.setText(i > 9 ? i + "" : "0" + i);
                                        nameTv.setText(mapReachVoice.getReachName());
                                        indexTv.setSelected(position == runPosition);
                                        String imagePath = mapReachVoice.getImagePath();
                                        if (!TextUtils.isEmpty(imagePath)) {
                                                imagePath = imagePath.replace("/uupic/", "/uupic_thumb/");
                                                GlideApp.with(GuideSetPointerActivity.this)
                                                        .asDrawable()
                                                        .load(IpMsgConst.HTTP_URL_CHEST + imagePath)
                                                        .thumbnail(0.1f)
                                                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                                                        .skipMemoryCache(false)
                                                        .placeholder(R.drawable.point_image_default)//占位图
                                                        .error(R.drawable.point_image_default)
                                                        .override(100, 100)
                                                        .into(roundedImageView);
                                        }

                                        break;
                                case 1:
                                        break;
                                default:
                        }
                }
        }


        class MyCallBack extends ItemTouchHelper.Callback {

                private boolean isDelete;

                private RecyclerAdapter adapter;

                private MyHolder itemViewHolder;

                public MyCallBack(RecyclerAdapter adapter) {
                        this.adapter = adapter;
                }

                @Override
                public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                        int swipeFlags = 0;
                        return makeMovementFlags(dragFlags, swipeFlags);
                }

                @Override
                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                        if (target.getItemViewType() == 1) {
                                return true;
                        }
                        int adapterPosition = viewHolder.getAdapterPosition();
                        Log.e("Test", "adapterPosition:" + adapterPosition);
                        MapReachVoice remove = mapReachVoices.remove(adapterPosition);
                        mapReachVoices.add(target.getAdapterPosition(), remove);
                        adapter.notifyItemMoved(adapterPosition, target.getAdapterPosition());
                        return true;
                }

                /**
                 * 滑动删除
                 *
                 * @param viewHolder viewHolder
                 * @param direction  direction
                 */
                @Override
                public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                }

                /**
                 * 长按拖长按拖拽删除 true  自己处理 false 调用 itemTouchHelper.startDrag(holder);
                 */
                @Override
                public boolean isLongPressDragEnabled() {
                        return false;
                }

                /**
                 * 滑动删除
                 *
                 * @return false
                 */
                @Override
                public boolean isItemViewSwipeEnabled() {
                        return false;
                }


                /**
                 * 当拖拽开始的时候调用
                 *
                 * @param viewHolder  viewHolder
                 * @param actionState actionState
                 */
                @Override
                public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                        //拖拽的时候
                        if (actionState == ItemTouchHelper.ACTION_STATE_IDLE) {
                                isDelete = true;
                                int[] ints = new int[2];
                                int[] ints2 = new int[2];
                                pointerDeleteIv.getLocationOnScreen(ints);
                                int w = itemViewHolder.itemView.getWidth();
                                int h = itemViewHolder.itemView.getHeight();
                                itemViewHolder.itemView.getLocationOnScreen(ints2);
                                Rect rect = new Rect(ints2[0], ints2[1], ints2[0] + w, ints2[1] + h);
                                Log.e("onItemClear", "rect:" + rect);
                                boolean contains = rect.contains(ints[0], ints[1]);
                                Log.e("onItemClear", "contains:" + contains);
                                if (contains) {
                                        int adapterPosition = itemViewHolder.getAdapterPosition();
                                        Log.e("onItemClear", "contains:" + adapterPosition);
                                        mapReachVoices.remove(adapterPosition);
                                        itemTouchHelper.onChildViewDetachedFromWindow(itemViewHolder.itemView);
                                        adapter.notifyItemRemoved(adapterPosition);
                                } else {
                                        isDelete = false;
                                }
                        }
                        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                                isDelete = false;
                                if (viewHolder instanceof MyHolder) {
                                        //让ViewHolder知道Item开始选中
                                        itemViewHolder = (MyHolder) viewHolder;
                                }
                        }
                        super.onSelectedChanged(viewHolder, actionState);
                }

                @Override
                public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
                                        boolean isCurrentlyActive) {
                        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
                                if (isDelete) {
                                        return;
                                }
                                int[] ints = new int[2];
                                int[] ints2 = new int[2];
                                pointerDeleteIv.getLocationOnScreen(ints);
                                int w = viewHolder.itemView.getWidth();
                                int h = viewHolder.itemView.getHeight();
                                viewHolder.itemView.getLocationOnScreen(ints2);
                                layoutParams.x = ints2[0];
                                layoutParams.y = ints2[1];
                                windowManager.updateViewLayout(dragImageView, layoutParams);
                                Rect rect = new Rect(ints2[0], ints2[1], ints2[0] + w, ints2[1] + h);
                                Log.e("onItemClear", "rect:" + rect);
                                boolean contains = rect.contains(ints[0], ints[1]);
                                Log.e("onItemClear", "contains:" + contains);
                                pointerDeleteIv.setSelected(contains);

                        }

                        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
                }

                @Override
                public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
                                            boolean isCurrentlyActive) {

                        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
                                if (isDelete) {
                                        return;
                                }
                                int[] ints = new int[2];
                                int[] ints2 = new int[2];
                                pointerDeleteIv.getLocationOnScreen(ints);
                                int w = viewHolder.itemView.getWidth();
                                int h = viewHolder.itemView.getHeight();
                                viewHolder.itemView.getLocationOnScreen(ints2);
                                layoutParams.x = ints2[0];
                                layoutParams.y = ints2[1];
                                windowManager.updateViewLayout(dragImageView, layoutParams);
                                Rect rect = new Rect(ints2[0], ints2[1], ints2[0] + w, ints2[1] + h);
                                Log.e("onItemClear", "rect:" + rect);
                                boolean contains = rect.contains(ints[0], ints[1]);
                                Log.e("onItemClear", "contains:" + contains);
                                pointerDeleteIv.setSelected(contains);
                        }
                        super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
                }

                /**
                 * 当拖拽结束的时候调用
                 *
                 * @param recyclerView
                 * @param viewHolder
                 */
                @Override
                public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                        super.clearView(recyclerView, viewHolder);
                        pointerDeleteIv.setSelected(false);
                        windowManager.removeView(dragImageView);
                        viewHolder.itemView.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                }
        }

        private WindowManager windowManager;

        private WindowManager.LayoutParams layoutParams;

        private ImageView dragImageView;

        private void showWindowView(View itemView) {
                itemView.setDrawingCacheEnabled(true);
                Bitmap drawingCache = itemView.getDrawingCache();
                layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
                dragImageView = new ImageView(this);
                dragImageView.setImageBitmap(drawingCache);
                int[] ints = new int[2];
                itemView.getLocationOnScreen(ints);
                layoutParams.x = ints[0];
                layoutParams.y = ints[1];
                layoutParams.width = itemView.getWidth();
                layoutParams.height = itemView.getHeight();
                layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
                layoutParams.format = PixelFormat.TRANSLUCENT;
                layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
                layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN;
                windowManager.addView(dragImageView, layoutParams);
                itemView.setVisibility(View.INVISIBLE);
        }
}
